package com.groupten.MyController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.groupten.Entity.ConfigureRuleEntity;
import com.groupten.Entity.PaperEntity;
import com.groupten.Entity.QuestionEntity;
import com.groupten.Entity.WqHouseEntity;
import com.groupten.Service.ConfigureRuleService;
import com.groupten.Service.PaperService;
import com.groupten.ServiceImpl.ConfigureRuleServiceImpl;
import com.groupten.ServiceImpl.PaperServiceImpl;
import com.groupten.ServiceImpl.WqHouseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("group10/hbjPaper")
public class HbjPaperController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private ConfigureRuleService configureRuleService;
    @Autowired
    private WqHouseServiceImpl wqHouseService;

    @GetMapping("deleteObsoleteQuestionInWq")
    public void deleteObsoleteQuestionInWq(){
        wqHouseService.deleteObsoleteQuestion();
    }

    @GetMapping("distinctWq")
    public Boolean distinctWq(){
        wqHouseService.distinct();
        return true;
    }

    @GetMapping("insertWq")
    public Boolean insertWq(Integer questionId, Integer subject, Integer knowledgePoint, Integer questionType, Double score, Double level, String studentNumber){
        WqHouseEntity wrongQuestionHouse = new WqHouseEntity();
        wrongQuestionHouse.setQuestionId(questionId);
        wrongQuestionHouse.setSubject(subject);
        wrongQuestionHouse.setKnowledgePoint(knowledgePoint);
        wrongQuestionHouse.setQuestionType(questionType);
        wrongQuestionHouse.setScore(score);
        wrongQuestionHouse.setLevel(level);
        wrongQuestionHouse.setStudentNumber(studentNumber);
        return wqHouseService.insertWqHouse(wrongQuestionHouse)>0;
    }

    @RequestMapping("generatePaperByRule")
    public IPage<QuestionEntity> generatePaper(@RequestBody Map<String,String> generateInfo){
        String subject = generateInfo.get("subject");
        Integer session = Integer.valueOf(generateInfo.get("session"));
        Integer page = null;
        Integer limit = null;
        ConfigureRuleEntity ruleEntity = setRule(generateInfo);
        if (generateInfo.containsKey("page")){
            if (generateInfo.get("page")!=null){
                page = Integer.valueOf(generateInfo.get("page"));
            }
        }
        if (generateInfo.containsKey("limit")){
            if (generateInfo.get("limit")!=null){
                limit = Integer.valueOf(generateInfo.get("limit"));
            }
        }
        return paperService.generatePaperByRule(subject, session, ruleEntity, page, limit, LocalDateTime.now());
    }

    @RequestMapping("generatePaperByKnowledgePoints")
    public IPage<QuestionEntity> generatePaperByKnowledgePoint(@RequestBody Map<String,String> generateInfo){
        String[] knowledgePoints = generateInfo.get("knowledgePoints").split(",");
        Integer session = Integer.valueOf(generateInfo.get("session"));
        Integer page = null;
        Integer limit = null;
        ConfigureRuleEntity ruleEntity = setRule(generateInfo);
        if (generateInfo.containsKey("page")){
            if (generateInfo.get("page")!=null){
                page = Integer.valueOf(generateInfo.get("page"));
            }
        }
        if (generateInfo.containsKey("limit")){
            if (generateInfo.get("limit")!=null){
                limit = Integer.valueOf(generateInfo.get("limit"));
            }
        }
        return paperService.generatePaperByKnowledgePoint(knowledgePoints, session, ruleEntity, page, limit, LocalDateTime.now());
    }

    @RequestMapping("generatePaperByWq")
    public IPage<QuestionEntity> generatePaperByWq(@RequestBody Map<String,String> generateInfo){
        String stuNumber = generateInfo.get("studentNumber");
        String subject = generateInfo.get("subject");
        Integer session = Integer.valueOf(generateInfo.get("session"));
        Integer page = null;
        Integer limit = null;
        ConfigureRuleEntity ruleEntity = setRule(generateInfo);
        if (generateInfo.containsKey("page")){
            if (generateInfo.get("page")!=null){
                page = Integer.valueOf(generateInfo.get("page"));
            }
        }
        if (generateInfo.containsKey("limit")){
            if (generateInfo.get("limit")!=null){
                limit = Integer.valueOf(generateInfo.get("limit"));
            }
        }
        return paperService.generatePaperByWq(stuNumber, subject, session, ruleEntity, page, limit, LocalDateTime.now());
    }

    @GetMapping("deleteObsoleteQuestionInPaper")
    public void deleteObsoleteQuestion(){
        paperService.deleteObsoleteQuestion();
    }

    @GetMapping("selectBySession")
    public IPage<PaperEntity> selectBySession(Integer session){
        return paperService.selectBySession(session,1,10);
    }

    public ConfigureRuleEntity setRule(Map<String,String> generateInfo){
        ConfigureRuleEntity ruleEntity = null;
        if (!generateInfo.containsKey("rule") && !generateInfo.containsKey("singlechoiceNumber") && !generateInfo.containsKey("multiplechoiceNumber") && !generateInfo.containsKey("blankNumber")){
            //此时没有指定规则和自定义规则，采用默认生成规则，如果有指定passingRate，page，limit则进行设置
            ruleEntity = configureRuleService.selectById(1L);
        }else if (generateInfo.containsKey("rule")){
            //此时指定了规则
            ruleEntity = configureRuleService.selectByName(generateInfo.get("rule"));
            if (ruleEntity==null){
                throw new RuntimeException("规则不存在");
            }
        }else {
            //此时指定了自定义规则，采用自定义规则，如果有指定passingRate，page，limit则进行设置
            ruleEntity = configureRuleService.selectById(2L);
        }
        for (Map.Entry<String, String> entry : generateInfo.entrySet()) {
            switch (entry.getKey()){
                case "singlechoiceNumber":
                    if (entry.getValue() != null){  //如果是null则不设置，采用默认值0
                        ruleEntity.setSinglechoiceNumber(Integer.valueOf(entry.getValue()));
                    }
                    break;
                case "multiplechoiceNumber":
                    if (entry.getValue() != null){
                        ruleEntity.setMultiplechoiceNumber(Integer.valueOf(entry.getValue()));
                    }
                    break;
                case "blankNumber":
                    if (entry.getValue() != null){
                        ruleEntity.setBlankNumber(Integer.valueOf(entry.getValue()));
                    }
                    break;
                case "passingRate":
                    if (entry.getValue() != null){
                        ruleEntity.setPassingRate(Double.valueOf(entry.getValue()));
                    }
                    break;
                default:
                    break;
            }
        }
        return ruleEntity;
    }

}
